package com.duan.controller;

import com.duan.base.JSONResponse;
import com.duan.pojo.User;
import com.duan.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class RecordController {

    private final RecordService recordService;

    @Autowired
    public RecordController(RecordService recordService){
        this.recordService = recordService;
    }

    /**
     * 增加抽奖记录
     * @param awardId 此次抽奖奖项ID
     * @param memberIds 此次抽奖中奖人员集合
     * @param session 上下文
     * @param resp 响应
     * @return json信息
     */
    @PostMapping("/api/record/add/{awardId}")
    public JSONResponse add(@PathVariable Integer awardId, @RequestBody List<Integer> memberIds, HttpSession session, HttpServletResponse resp){
        User user = (User)session.getAttribute("currentUser");
        JSONResponse jsonResponse = new JSONResponse();
        if(user == null){
            resp.setStatus(401);
            jsonResponse.setSuccess(false);
            jsonResponse.setCode("USR000");
            jsonResponse.setMessage("用户没有登录，不允许访问");
            return jsonResponse;
        }

        int res = recordService.add(memberIds, awardId);
        jsonResponse.setSuccess(true);
        return jsonResponse;
    }

    /**
     * 通过奖项id删除指定记录
     * @param id awardId
     * @param session 上下文
     * @param resp 响应
     * @return json格式文件
     */
    @GetMapping("/api/record/delete/award")
    public JSONResponse deleteByAwardId(Integer id, HttpSession session, HttpServletResponse resp){
        User user = (User)session.getAttribute("currentUser");
        JSONResponse jsonResponse = new JSONResponse();
        if(user == null){
            resp.setStatus(401);
            jsonResponse.setSuccess(false);
            jsonResponse.setCode("USR000");
            jsonResponse.setMessage("用户没有登录，不允许访问");
            return jsonResponse;
        }

        int n = recordService.deleteByAwardId(id);
        jsonResponse.setSuccess(true);
        return jsonResponse;
    }

    /***
     * 删除指定中奖人员的记录
     * @param id memberId
     * @param session 上下文
     * @param resp 响应
     * @return json格式文件
     */
    @GetMapping("/api/record/delete/member")
    public JSONResponse deleteByMemberId(Integer id, HttpSession session, HttpServletResponse resp){
        User user = (User)session.getAttribute("currentUser");
        JSONResponse jsonResponse = new JSONResponse();

        if(user == null){
            resp.setStatus(401);
            jsonResponse.setSuccess(false);
            jsonResponse.setCode("USR000");
            jsonResponse.setMessage("用户没有登录，不允许访问");
            return jsonResponse;
        }

        int res = recordService.deleteByMemberId(id);
        jsonResponse.setSuccess(true);
        return jsonResponse;
    }



    /***
     * 删除此次抽奖的所有中奖记录
     * @param session 通过userId，连表删除掉此次setting中的awardId和memeberId对应的所有记录
     * @return 结果
     */
    @GetMapping("/api/record/delete/setting")
    public JSONResponse deleteBySetting(HttpSession session, HttpServletResponse resp){
        User user = (User)session.getAttribute("currentUser");
        JSONResponse jsonResponse = new JSONResponse();

        if(user == null){
            resp.setStatus(401);
            jsonResponse.setSuccess(false);
            jsonResponse.setCode("USR000");
            jsonResponse.setMessage("用户没有登录，不允许访问");
            return jsonResponse;
        }

        int res = recordService.deleteByUserId(user.getId());
        jsonResponse.setSuccess(true);
        return jsonResponse;
    }
}
